package com.bootdo.reverseproxy.mask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class MaskService {

    @Autowired
    private MaskMethod maskMethod;

    @Autowired
    private RegexString regexString;

    public String mask(String sourceContent) {
        Map<String, String> regex = regexString.getRegexString();
        Map<String, List<String>> findAll = new HashMap<>();
        for (Map.Entry<String, String> each : regex.entrySet()) {
            List<String> list = new ArrayList<>();
            Pattern pattern = Pattern.compile(each.getValue());
            Matcher matcher = pattern.matcher(sourceContent);
            while (matcher.find()) {
                list.add(matcher.group());
            }
            findAll.put(each.getKey(), list);
        }
        String replacedsourceContent;
        for (Map.Entry<String, List<String>> entry : findAll.entrySet()) {
            String replacement;
            switch (entry.getKey()) {
                case "phone":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.baseMask(each, 8, 4);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "fixedTel":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.baseMask(each, (each.indexOf("-") + 4), 4);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "id":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.baseMask(each, 15, 8);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "stuNum":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.baseMask(each, each.length() - 3, 4);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "birth":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.birthMask(each);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "email":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.emailMask(each, 10, 4);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "schoolName":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.schoolNameMask(each);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "personName":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.nameMask(each);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
                case "address":
                    for (String each : entry.getValue()) {
                        replacement = maskMethod.adressMask(each);
                        replacedsourceContent = sourceContent.replaceAll(each, replacement);
                        sourceContent = replacedsourceContent;
                    }
                    break;
            }
        }
        return  sourceContent;
    }
}
